package com.coscon.utils;

import org.apache.commons.lang3.StringUtils;

import java.security.Key;
import java.util.Map;

/**
 * 
* @ClassName: LoginPagePasswordDecrypt 
* @Description: TODO(登录界面的密码解密) 
* @author 409390047@qq.com 
* @date 2018年10月29日 上午10:51:59
 */
public class WfLoginPagePasswordDecrypt {
	
	static String publicKey  = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCWfg8yG3uszyJ/OaWbr28Biwy//KLrn4347/VSX71NZKJpUFp/97d6OjxxLxJ2zxJ3ZpKBnGubLQj5ETCuYTQc0toKGkrB8MnG9plubfsNMdVgBKStBVhqjvZ0cwxqvffo4TxQTRzvifaj6a9ANHhSrycCQD1glFR/qEhyslpxUQIDAQAB";
	//私钥,对应js文件的公钥
	//采用js文件中的公钥加密，私钥解密
	static String  privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJZ+DzIbe6zPIn85pZuvbwGLDL/8ouufjfjv9VJfvU1komlQWn/3t3o6PHEvEnbPEndmkoGca5stCPkRMK5hNBzS2goaSsHwycb2mW5t+w0x1WAEpK0FWGqO9nRzDGq99+jhPFBNHO+J9qPpr0A0eFKvJwJAPWCUVH+oSHKyWnFRAgMBAAECgYA5b1r1UK/ewB72V1XxGP9Qo3T2QMEdFAPnIg2KpqdkxieF8fjYJf6MU3WF7OUrGtR9ZVQ8yyn2SzXFeN4Pb2GnmIzYkAvuJAPxhHZ12osnYyQeWG7WUH/705uKH0zM+c6jGaZ14c2XRZ6xTc3mRP4n5k65foxm1WRfMChG+q09AQJBAMy39+30Gaxjk7zrMbbGEX/M4j9tvBUdeeefZJKbrFCySTrljYF2eywJCnvfPk9kYQTE5vu3hUawziRYp+X9USkCQQC8MLdbvgzbsYPANfi+kN/fmtZ7goeliBF99LSh734Ku4XFw98qGuC+fH3+bAfCuX9ldCnJ2J+3enXmm8AfolvpAkEAt1I6u6QuZVgaUT+QGisOOpJ/O+uJVKM7qfXrOvoaeXzd5NbxO7CcphdGgxyrkK7DzmCAs4Jo35+uSY/2chbA2QJASXkvrVbnCEMnMD0iL6y6az+1q6ULtroIMkf+2hZ5/FC4ar0NfQ/fXR+XxWSCJylrLr4Krq6vtN/P9kgq2271OQJBAImvrsJqu2vx54bAI5idcXXevU9uE4PK3ylOQIUFiaE+wNXKgrv7T7+RRM3i6dThvEIByPQBwvAIO8x/VQoJK2E=";
	
	/**
	 * 
	* @Title: encryption 
	* @Description: TODO(根据公钥加密) 
	* @param @param mingText
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
	public static String encryption(String mingText){
		RSACodeUtil rsac = new RSACodeUtil();
		byte[] miDataByte = null;
		try {
			miDataByte = rsac.encryptByPublicKey(mingText, publicKey);
		} catch (Exception e) {
			e.printStackTrace();
		}
		String mi  =rsac.encryptBase64(miDataByte);
		return mi;
	}
	
	
	/**
	* @Title: decrypt 
	* @Description: TODO(根据私钥解密) 
	* @param @param miText
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
	public static String decrypt(String miText){
		String ming= null;
		if(StringUtils.isBlank(miText)){
			return ming;
		}
		RSACodeUtil rsac = new RSACodeUtil();
		byte[] decodedData;
		try {
			decodedData = rsac.decryptByPrivateKey( 
					rsac.decryptBase64(miText),
					privateKey
			);
			ming = new String(decodedData);
		} catch (Exception e) {
		}
		return ming;
	}

	public static void main(String[] args) throws Exception {

		RSACodeUtil rsac = new RSACodeUtil();
		//初始化公钥与私钥
		Map<String, Key> map = rsac.initKey();
		//得到公钥
		String publicKey = rsac.getPublicKey(map);
		//得到私钥
		String privateKey = rsac.getPrivateKey(map);
		System.out.println("publicKey=" + publicKey);
		System.out.println("privateKey=" + privateKey);



	}
	
	
}
